﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Web.BaseCode;
using Maticsoft.Common;

namespace Web
{
    public partial class Category : System.Web.UI.Page
    {
        private int pageSize = BaseCode.CommValue.PageSize;
        public string TagName = string.Empty;
        public string TagUrl = string.Empty;
        public string KeyWords = string.Empty;
        public String Description = string.Empty;
        protected string TitlePage = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BindData();
            }
        }

        protected void AspNetPager1_PageChanged(object src, EventArgs e)
        {
            BindData();
        }

        /// <summary>
        /// 显示文章列表
        /// </summary>
        void BindData()
        {
            if (!this.GetArticles())
            {
                //404跳转
            }
        }

        /// <summary>
        /// 网站首页文章列表
        /// </summary>
        private bool GetArticles()
        {
            string ename = string.Empty;
            int pageid = 1;

            if (Request.QueryString["ename"] != null)
            {
                ename = Request.QueryString["ename"].ToString();
            }
            if (Request.QueryString["pageid"] != null)
            {
                string pageidStr = Request.QueryString["pageid"].ToString();
                if (!int.TryParse(pageidStr, out pageid))
                    pageid = 1;
            }

            if (pageid > 1)
                this.TitlePage = string.Format(" | 第{0}页", pageid);

            //获取分类名称
            BLL.BLL_Tags bllTag = new BLL.BLL_Tags();
            Model.Model_Tags modelTag = bllTag.GetModel(ename);
            if (modelTag != null)
            {
                TagName = modelTag.name;
                this.TagUrl = BaseCode.CommMethod.GetUrl(BlogPageType.Category, modelTag.ename);
                this.KeyWords = modelTag.keywords;
                this.Description = modelTag.remark;

                //获取分类下的文章
                BLL.BLL_Article bll = new BLL.BLL_Article();
                string strWhere = string.Format("id in (select articleid from tags_art where tagsid ={0})", modelTag.id);

                System.Data.DataSet ds = bll.GetPageList(pageSize, pageid, strWhere);

                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    this.RepeaterArticleList.DataSource = ds;
                    this.RepeaterArticleList.DataBind();
                }

                int totalCount = bll.GetCount(strWhere);
                if (totalCount > this.pageSize)
                {
                    int totalPageCount = totalCount / this.pageSize;
                    if (totalCount % this.pageSize > 0)
                        totalPageCount++;

                    this.ltr_paging.Text = CommMethod.GetPaging(BlogPageType.Category, totalPageCount, pageid, modelTag.ename);
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 文章列表绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void RepeaterArticleList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView rowv = (DataRowView)e.Item.DataItem;
                string ename = rowv["ename"].ToString();
                string contents = rowv["contents"].ToString();
                Literal ltr_ArticleUrl = e.Item.FindControl("ltr_ArticleUrl") as Literal;
                Literal ltr_ArticleUrl2 = e.Item.FindControl("ltr_ArticleUrl2") as Literal;
                Literal ltr_contents = e.Item.FindControl("ltr_contents") as Literal;

                ltr_ArticleUrl.Text = CommMethod.GetUrl(BlogPageType.Article, ename);
                ltr_ArticleUrl2.Text = ltr_ArticleUrl.Text;
                ltr_contents.Text = HtmlHelper.SubstringToHTML(contents, BaseCode.CommValue.ArticleLength, "... ...");
            }
        }
    }
}